<?php

namespace App\Services\Menu;

use App\Models\Category;
use App\Models\Menu;

/**
 * 菜单服务
 * @package App\Services\Menu
 */
class MenuService
{
    public function generate()
    {
        $categories = Category::all();

    }


    public static function getChildren($arr, $pid = 0)
    {
        $tree = [];
        foreach ($arr as $key => $v) {
            if ($v['pid'] == $pid) {
                $v['children'] = self::getChildren($arr, $v['id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }

    public static function setTree($arr)
    {

        foreach ($arr as $key => $item) {
            Menu::updateOrCreate(
                [
                    'name' => $item['name'],
                    'title' => $item['title'],
                    'icon' => $item['icon'],
                    'path' => $item['path']
                ]
            );
            if (array_key_exists('children', $item)) {
                static::setTree($item['children']);
            }
        }
        return Menu::all();
    }

}
